package com.cuitrip.util;

import android.app.Activity;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RatingBar;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.ViewSwitcher;

import com.cuitrip.business.tripservice.ui.IconTextView;
import com.lab.logtrack.AppLog;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by ak
 * UI操作工具类
 */
public class UIUtil {

    public static View findViewById(Activity activity, int id) {
        return findViewById(activity, id, View.class);
    }

    public static Button findButtonById(Activity activity, int id) {
        return findViewById(activity, id, Button.class);
    }



    public static ImageView findImageViewById(Activity activity, int id) {
        return findViewById(activity, id, ImageView.class);
    }

    public static IconTextView findIconTextViewById(Activity activity, int id) {
        return findViewById(activity, id, IconTextView.class);
    }

    public static TextView findTextViewById(Activity activity, int id) {
        return findViewById(activity, id, TextView.class);
    }

    public static EditText findEditTextById(Activity activity, int id) {
        return findViewById(activity, id, EditText.class);
    }

    public static WebView findWebViewById(Activity activity, int id) {
        return findViewById(activity, id, WebView.class);
    }

    public static ViewPager findViewPagerById(Activity activity, int id) {
        return findViewById(activity, id, ViewPager.class);
    }


    public static ListView findListViewById(Activity activity, int id) {
        return findViewById(activity, id, ListView.class);
    }

    public static GridView findListGridViewById(Activity activity, int id) {
        return findViewById(activity, id, GridView.class);
    }

    public static RatingBar findRatingBarById(Activity activity, int id) {
        return findViewById(activity, id, RatingBar.class);
    }

    public static ScrollView findScrollViewById(Activity activity, int id) {
        return findViewById(activity, id, ScrollView.class);
    }

    public static View findViewById(View view, int id) {
        return findViewById(view, id, View.class);
    }

    public static Button findButtonById(View view, int id) {
        return findViewById(view, id, Button.class);
    }

    public static ImageView findImageViewById(View view, int id) {
        return findViewById(view, id, ImageView.class);
    }

    public static CircleImageView findCircleImageView(Activity activity, int id) {
        return findViewById(activity, id, CircleImageView.class);
    }

    public static TextView findTextViewById(View view, int id) {
        return findViewById(view, id, TextView.class);
    }

    public static EditText findEditTextById(View view, int id) {
        return findViewById(view, id, EditText.class);
    }

    public static ScrollView findScrollViewById(View view, int id) {
        return findViewById(view, id, ScrollView.class);
    }

    public static ListView findListViewById(View view, int id) {
        return findViewById(view, id, ListView.class);
    }

    public static RatingBar findRatingBarById(View view, int id) {
        return findViewById(view, id, RatingBar.class);
    }

    public static ViewSwitcher findViewSwitcherById(View view, int id) {
        return findViewById(view, id, ViewSwitcher.class);
    }

    public static ViewPager findViewPagerById(View view, int id) {
        return findViewById(view, id, ViewPager.class);
    }

    public static <T> T findViewById(Activity activity, int id, Class<T> viewType) {
        try {
            return (T) activity.findViewById(id);
        } catch (Exception e) {
            AppLog.w(e.getMessage());
        }
        return null;
    }

    public static <T> T findViewById(View view, int id, Class<T> viewType) {
        try {
            return (T) view.findViewById(id);
        } catch (Exception e) {
            AppLog.w(e.getMessage());
        }
        return null;
    }

    /**
     * 自动化埋点
     * @param view
     * @param listener
     */
    private static void bindViewListener(View view, View.OnClickListener listener) {
        try {
            view.setOnClickListener(listener);
        } catch (Exception e) {
            AppLog.w(e.getMessage());
        }
    }

    public static void bindClickListener(View view, View.OnClickListener listener) {
        bindViewListener(view, listener);
    }

    public static void bindClickListener(View.OnClickListener listener, View... views) {
        try {
            if (null != views) {
                for (View view : views) {
                    if (null != view)
                        bindViewListener(view, listener);
                }
            }
        } catch (Exception e) {
            AppLog.w(e.getMessage());
        }
    }

    /**
     * 根据id绑定OnClickListener
     *
     * @param rootView 根View
     * @param listener View.OnClickListener
     * @param ids      R.id
     */
    public static void bindClickListener(View rootView, View.OnClickListener listener, int... ids) {
        try {
            for (int i : ids) {
                View view = rootView.findViewById(i);
                if (null != view)
                    bindViewListener(view, listener);
            }
        } catch (Exception e) {
            AppLog.w(e.getMessage());
        }
    }

    public static void bindClickListener(Activity activity, View.OnClickListener listener, int... ids) {
        try {
            for (int i : ids) {
                View view = activity.findViewById(i);
                if (null != view)
                    bindViewListener(view, listener);
            }
        } catch (Exception e) {
            AppLog.w(e.getMessage());
        }
    }

    public static void bindLongClickListener(View view, View.OnLongClickListener listener) {
        try {
            view.setOnLongClickListener(listener);
        } catch (Exception e) {
            AppLog.w(e.getMessage());
        }
    }

    public static void bindLongClickListener(View.OnLongClickListener listener, View... views) {
        try {
            if (null != views) {
                for (View view : views) {
                    if (null != view)
                        view.setOnLongClickListener(listener);
                }
            }
        } catch (Exception e) {
            AppLog.w(e.getMessage());
        }
    }

    public static void bindKeyListener(View view, View.OnKeyListener listener) {
        try {
            view.setOnKeyListener(listener);
        } catch (Exception e) {
            AppLog.w(e.getMessage());
        }
    }

    public static void bindKeyClickListener(View.OnKeyListener listener, View... views) {
        try {
            if (null != views) {
                for (View view : views) {
                    if (null != view)
                        view.setOnKeyListener(listener);
                }
            }
        } catch (Exception e) {
            AppLog.w(e.getMessage());
        }
    }

}
